import { createStore } from 'vuex'

const now=new Date();

const store = createStore({
	// 声明
    state:{
		cnLog:"",
		pcapContent:"",
		imsLog1:"",
		imsLog2:"",
		imsLog3:"",
        sessions:[{
            id:1,
            user:{
                name:'韩婷',
            },
            messages:[{
                content:'网关配置&日志监控',
                date:now
            }]
        }],
        currentSessionId:1,
        filterKey:''
    },
	// 计算属性
    getters:{

    },
	// 数据同步更改
    mutations:{
		cnLog(state,r){
			state.cnLog=r
		},
		pcapContent(state,r){
			state.pcapContent=r
		},
		imsLog1(state,r){
			state.imsLog1=r
		},
		imsLog2(state,r){
			state.imsLog2=r
		},
		imsLog3(state,r){
			state.imsLog3=r
		},
        changeCurrentSessionId (state,id) {
            state.currentSessionId = id;
        },
        addMessage (state,msg) {
            state.sessions[state.currentSessionId-1].messages.push({
                content:msg,
                date: new Date(),
                self:true
            })
        },
        INIT_DATA (state) {
            let data = localStorage.getItem('vue-chat-session');
            //console.log(data)
            if (data) {
                state.sessions = JSON.parse(data);
            }
        }
    },
	// 异步修改
    actions:{
        initData (context) {
            context.commit('INIT_DATA')
        }
    }
})

store.watch(function (state) {
    return state.sessions
},function (val) {
    console.log('CHANGE: ', val);
    localStorage.setItem('vue-chat-session', JSON.stringify(val));
},{
    deep:true/*这个貌似是开启watch监测的判断,官方说明也比较模糊*/
})
export default store;
